﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Lonewolf.Entity;
using Lonewolf.Infrastructure;
using Lonewolf.IService;
using Microsoft.AspNetCore.Mvc;

namespace Lonewolf.WebApp.Controllers
{
    public class MenuController : AuthController
    {
        private IMenuInfoService menuInfo;
        public MenuController(IMenuInfoService menuInfo)
        {
            this.menuInfo = menuInfo;
        }

        public IActionResult Icon()
        {
            return View();
        }
        public IActionResult MenuList()
        {
            return View();
        }
        public IActionResult MenuForm()
        {
            return View();
        }
        [HttpPost]
        public JsonResult<TopMenuEntity> GetTopMenus()
        {
            var userId = User.FindFirst("Id").Value;
            return menuInfo.GetTopMenuList(userId);
        }
        [HttpPost]
        public JsonResult<MenuNavBar> GetLeftMenus(string id)
        {
            var userId = User.FindFirst("Id").Value;
            return menuInfo.GetLeftMenus(userId, id);
        }
        [HttpPost]
        public JsonResult<MenuInfo> GetButtons(string url)
        {
            var userId = User.FindFirst("Id").Value;
            return menuInfo.GetButtonByUrl(userId, url);
        }
        [HttpPost]
        public async Task<JsonResult<string>> Form(MenuInfo menu)
        {
            var userId = User.FindFirst("Id").Value;
            return await menuInfo.SaveAsync(menu, userId);
        }
        [HttpPost]
        public string GetModular()
        {
            var userId = User.FindFirst("Id").Value;
            return menuInfo.GetModularByUserId(userId).ToTreeSelectJson();
        }
        [HttpPost]
        public async Task<dynamic> GetPages(int page, int limit)
        {
            var pages = await menuInfo.GetPagesAsync(page, limit);
            return ObjectHelper.PageToLayuiTable(pages);
        }
        [HttpPost]
        public string GetForm(string pk)
        {
            var entity =  menuInfo.QueryNoTracking().FirstOrDefault(a => a.Id == pk);
            return entity.ToJson();
        }
    }
}